<?php
defined('DCS_ROOT') or exit(header("HTTP/1.1 430 Not Forbidden"));

include_once( '\..\..\index_weibo.php' );  //猜测：因为微博接口的index文件是记录发起路径，因此对于每个页面都需要引用


class main {
	
	function __construct() {
		//do something
	}

	function defaultAction(){		
		$tpl = new Template("main.htm" );
		require_once(APP_PATH."model/main.php");
		$db  = getDB();
		$data = new IndexModel($db);

		$play_movie = $data->getPlayMovie('playtime','8');
		
		if(!empty($play_movie)){
			foreach($play_movie as $k=>$v){
				$play_movie[$k]['playtime'] = date('Y-m-d',$v['playtime']);
				$play_movie[$k]['rating'] = getRating($v);
				$play_movie[$k]['rating_img'] = getStarImg($play_movie[$k]['rating']);			
				$play_movie[$k]['k'] = $k + 1;
			}
		}
		//de($play_movie);
		$tpl->assign("play_movie", $play_movie);

		//从短评列表，按时间排序取最近2周时间里的短评（所有在上映的电影），然后按时间倒序排列，取不超过100条。

		$hot_comments = $data->getComments('addtime',4);
		foreach($hot_comments as $k=>$v){
			$hot_comments[$k]['contents'] = html2Txt($v['contents']);
			$hot_comments[$k]['addtime'] = date('m月d日',$v['addtime']);
		}
		$tpl->assign("hot_comments", $hot_comments);

		//影评
		$hot_review = $data->getReviews('addtime',4);
		//de($hot_review);
		foreach($hot_review as $k=>$v){
			$hot_review[$k]['contents'] = csubstr(html2Txt($v['contents']),120);
			$hot_review[$k]['addtime'] = date('m月d日',$v['addtime']);
		}
		$tpl->assign("hot_review", $hot_review);

		$user = getMember();
		if($user){
			$uid = $user['uid'];
			//取 friend uid 列表
			$sql = "SELECT fid FROM member_friend WHERE uid = '$uid' LIMIT 0,8 ";
			$db->query($sql);
			$fid = $db->fetchAll();		
			foreach($fid as $k => $v){
				$fids .= "'".$v['fid']."',";
			}
			$fids = substr($fids,0,-1);

			//如果有关注的朋友
			if($fids){
				//朋友看过、想看的电影，其电影数据的时间范围是：当前上映+将上映的电影数据。并且电影按分组显示 查看朋友前后1个月的电影动态
				$friendCollect = $data->getFriendCollect($fids,10,2);
				$i = 0;
				foreach($friendCollect as $k => $v){					
					$friendCollects[$i]['uid'] = $v['uid'];
					$friendCollects[$i]['username'] = $v['username'];
					$friendCollects[$i]['avatars'] = $v['avatars'];
					$friendCollects[$i]['movie'] = $v['movie'];
					$i++;
				}
				$tpl->assign('friendCollect',$friendCollects);

				//朋友想看				
				$friendWish = $data->getFriendCollect($fids,20,1);
				$i = 0;
				foreach($friendWish as $k => $v){		
					$friendWishs[$i]['uid'] = $v['uid'];
					$friendWishs[$i]['username'] = $v['username'];
					$friendWishs[$i]['avatars'] = $v['avatars'];
					$friendWishs[$i]['movie'] = $v['movie'];
					$i++;
				}
				$tpl->assign('friendWish',$friendWishs);				
			}

			$var['islogin'] = 'yes';
		}
		
		$cache = DATA_PATH."cache/hotmovies.php";
		if(is_file($cache)){
			$hotmovies = require_once($cache);
		}
		if($hotmovies){
			foreach($hotmovies as $k => $v){
				$movieids .= "'".$v['id']."',";
			}
			$movieids = substr($movieids,0,-1);
			$hotmovies = '';
			$hotmovies = $data->getHotMovie($movieids,'playtime');
			//de($hotmovies);
			foreach($hotmovies as $k => $v){
				$hotmovies[$k]['rating'] = getRating($v);
				$hotmovies[$k]['rating_img'] = getStarImg($play_movie[$k]['rating']);
				$hotmovies[$k]['k'] = $k + 1;
				if($k == 0 ){
					$hotmovies[$k]['styles'] = 'first';
				}elseif($k == 3){
					$hotmovies[$k]['styles'] = 'last';
				}
			}
			$tpl->assign('hotmovies',$hotmovies);
		}
		$var['ftimes'] = time();

		$tpl->assign($var);
		$tpl->assign(assignUser());
		
		
	//	$tpl->assign('apiurl',$_SESSION['apiurl']);  //增加一行，把index记录的apiurl会话，指定到模板
		
		
		$tpl->assign("debuginfo",debuginfo().' 次查询');
		$tpl->output();

	}


}



?>
